$(function(){

  // 弹窗样式
  function layerCss(){
    $(".layui-layer").css("border-radius","10px")
    $(".layui-layer-setwin .layui-layer-close2").css({"right":"-40px","top":"0px"});
    $(".layui-layer-ico").css("background","url("+jumpUrl+"/img/question2/iconlayer.png)");
  }
// 我的订单
  var page = ''
  order_list_get(page);
  function order_list_get(page){
  	var order_url = jumpUrl+"/center/question/list";
    var loading = '<div class="loading">加载中...</div>';
    $(".order-box").append(loading);
  	$.get(order_url, function(data){
        },'json').error(function(data) {
          var status_code = $.parseJSON(data.responseText);
          layer.msg(status_code.message);
        }).success(function(data) {
        	$(".loading").remove();
           var obj = data.data;
           console.info(obj);
           if(obj.length>9){
            $(".item_pager .loadMore").css("display","block");
           }
            var str = '';
            if(obj.length ==0 ){
              str = '<div class="noData"></div><p class="noDatafont">还没有订单</p>';
              $(".order-box").append(str);
              return false;
            }
            for (var i = 0; i < obj.length; i++) {
              str+='<ul class="Whitebg10r">'  
                str+='<li class="Greytitle"><span>订单号：'+obj[i].order_no+'</span> <span class="fr">创建时间：'+obj[i].created_at+'</span></li>'
                str+='<li class="order-type">'
                  str+='<div class="lv-portrait"><img src="'+obj[i].lawyer_avatar+'" width="80" height="80"></div>'
                  str+='<div class="order-info">'
                    str+='<p><span class="font18 colorE">订单金额：</span>'+obj[i].price+'元</p>'
                    str+='<p><span class="font18 colorE">订单信息：</span>'+obj[i].type_name+'</p>'
                    str+='<p><span class="font18 colorE">订单状态：</span><span class="order-status-name">'+obj[i].status_name+'</span></p>'
                  str+='</div>'
                  if (obj[i].status_name == "待受理") {
                     str+='<div class="butbox">'
                     str+='</div>'
                  }else if (obj[i].status_name == "待确认"){
                    str+='<div class="butbox">'
                        str+='<span class="butD affirm" data-href="'+obj[i].id+'" style="margin-bottom: 10px;">确认完成</span>'
                        if (obj[i].type_name == "图文咨询") {
                          str+='<span class="butD to_ask" data-href="'+obj[i].id+'" >联系律师</span>'
                        }else{
                          str+='<span class="butD to_phone" data-href="'+obj[i].id+'" >联系律师</span>'
                        }
                        
                    str+='</div>'
                  }else if (obj[i].status_name == "待评价"){
                    str+='<div class="butbox">'
                      str+='<span class="butD top-comment" data-id="'+obj[i].lawyer_name+'" data-href="'+obj[i].id+'" style="margin-bottom: 10px;">服务评价</span>'
                    str+='</div>'
                  }else if (obj[i].status_name == "已完成"){
                    str+='<div class="butbox">'
                      str+='<span class="butD top-record" data-id="'+obj[i].lawyer_name+'" data-href="'+obj[i].id+'" style="margin-bottom: 10px;">聊天记录</span>'
                    str+='</div>'
                  }
                  
                str+='</li>'
                str+='<li class="contentbox oh">'
                  str+='<div class="order-viwe fl">'
                    str+='<div class="fl font18 colorE">订单信息：</div>'
                    str+='<div class="fl" style="width:400px;">'+obj[i].content+'</div>  '
                  str+='</div>'
                 str+=' <div class="fr">'
                    str+='<div class="js-open butopen">'
                      str+='<span class="textinfo">展开</span>'
                      str+='<span class="openIcon" style="background: url(http://lvtuan-pc-new/img/more1.png) 50% 50% no-repeat;"></span>'
                    str+='</div>'
                  str+='</div>'
                str+='</li>'
              str+='</ul>'
            };
          $(".order-box").append(str);
          $(document).on( "click", ".to_pay_ing", function(e) {
            var loading = layer.msg('加载中...', {icon: 16});
          });
          $(".js-open").click(function(){
    		  	var view_height = $(this).parent().parent().find(".order-viwe").height();
    		  	if (view_height == "0") {
    		  		$(this).parent().parent().find(".order-viwe").css("height","auto");
    		  		$(this).find(".textinfo").text("收缩");
    		  		$(this).find(".openIcon").css("background"," url("+jumpUrl+"/img/more1.png) center no-repeat");

    		  	}else{
      				$(this).parent().parent().find(".order-viwe").css("height","0px");
      				$(this).find(".openIcon").css("background"," url("+jumpUrl+"/img/more.png) center no-repeat");
      				$(this).find(".textinfo").text("展开");
    		  	}
		      });
    });
	
  }
    //滚动条拉到底部的时候去获取新的数据
  $(document).on("click",".loadMore",function(e){
      $(".item_pager .loadMore").css("display","none");
      var cuA = parseInt($(".loadmore").attr("data-href"));
      $(".loadmore").attr("data-href",cuA+1);
      var page = cuA+1;
      commodity_url(page);
      stopDefault(e);
  });

  $(".js-open").click(function(){
      var view_height = $(this).parent().parent().find(".order-viwe").height();
      if (view_height == "0") {
        $(this).parent().parent().find(".order-viwe").css("height","auto");
        $(this).find(".textinfo").text("收缩");
        $(this).find(".openIcon").css("background"," url("+jumpUrl+"/img/more1.png) center no-repeat");

      }else{
        $(this).parent().parent().find(".order-viwe").css("height","0px");
        $(this).find(".openIcon").css("background"," url("+jumpUrl+"/img/more.png) center no-repeat");
        $(this).find(".textinfo").text("展开");
      }
    });
  
  $(document).on( "click", ".js_Popup03", function(e) {
    surePay();
    var id = $(this).attr("data-href");
    var $this = $(this);
    var url = jumpUrl+"/center/question/"+id+"/cancel"
    $(document).on('click', '#js-pay-now', function(e){
      $.post(url,{},function(data){
          },'json').error(function(data) {
              var obj = $.parseJSON(data.responseText);
              layer.msg(obj.error_messages);
          }).success(function(data) {
              layer.msg("取消成功");
              window.location.href=skipUrl;
          });
          stopDefault(e);
    });
  });
  $(document).on('click', '.js-close', function(e){
    layer.closeAll(); 
  });


  //弹出确认取消订单
  function surePay(){
    var Popup03 = $(".Popup03");
        layer.open({
          type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
          title: false, //标题
          closeBtn: true,  //关闭按钮
          shade:0.3,  //遮罩默认是0.3透明度
          area: ['300px', '120px'],
          skin: 'yourclass', //样式类名
          content: Popup03 //内容 
      });
  };


  //确认完成1
  $(document).on( "click", ".affirm", function(e) {
    confirmComplete();
    var id = $(this).attr("data-href");
    $(".js_to_complete").attr("data-href",id);
  });
  //确认完成
  $(document).on( "click", ".js_to_complete", function(e) { 
    var loading = layer.msg('操作进行中...', {icon: 16});
    var user_id = $(this).attr("data-href");
    var url = jumpUrl+"/center/question/"+user_id+"/confirm";
      $.post(url,function(data){
        },'json').error(function(data) {
          var reeoe = data;
        }).success(function(data) {
            layer.msg(data.data);
            layer.closeAll();
            $("[data-href="+user_id+"]").parent().parent().find(".order-status-name").text("待评价");
            $("[data-href="+user_id+"]").parent(".butbox").html('<span class="butD top-comment" data-href="'+user_id+'" style="margin-bottom: 10px;">服务评价</span>');
          
        });
        stopDefault(e);
  });
  //弹出确认完成
  function confirmComplete(){
    var Popup01 = $(".Popup01");
        layer.open({
          type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
          title: false, //标题
          closeBtn: true,  //关闭按钮
          shade:0.3,  //遮罩默认是0.3透明度
          area: ['350px', '300px'],
          skin: 'yourclass', //样式类名
          content: Popup01 //内容 
      });
      layerCss();
  };

  //联系律师--图文
  $(document).on( "click", ".to_ask", function(e) {
    id = $(this).attr("data-href");
    ContactLawyer();
  });
  //弹出确认完成
  function ContactLawyer(){
    var Popup02 = jumpUrl+'/center/question/'+id+'/ask';
        layer.open({
          type: 2, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
          title: false, //标题
          closeBtn: true,  //关闭按钮
          shade:0.3,  //遮罩默认是0.3透明度
          area: ['570px', '610px'],
          skin: 'yourclass', //样式类名
          content: Popup02 //内容 
      });
      layerCss();
  };

  //联系律师--电话
  $(document).on( "click", ".to_phone", function(e) {
    var post_id = $(this).attr("data-href");
    ContactLawyer_phone();
    $.get(jumpUrl+"/callback/"+post_id,function(data){
      },'json').error(function(data){
        layer.closeAll(); 
      }).success(function(data){
        
        setTimeout(function(){
          layer.closeAll();
        }, 5000);

    });
     
  });
  //弹出联系律师
  function ContactLawyer_phone(){
    var Popup021 = $(".Popup021");
        layer.open({
          type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
          title: false, //标题
          closeBtn: true,  //关闭按钮
          shade:0.3,  //遮罩默认是0.3透明度
          area: ['350px', '300px'],
          skin: 'yourclass', //样式类名
          content: Popup021 //内容 
      });
      layerCss();
  };


  //聊天记录
  $(document).on( "click", ".top-record", function(e) {

    $(".ChatRecordbox").html("");
    ChatRecord();
    var id = $(this).attr("data-href");
    var lawyer_name = $(this).attr("data-id");
    var $this = $(this);
    var loading = '<div class="loading" style="margin-top:40px;">加载中...</div>';
    $(".ChatRecordbox").append(loading);
    var url = jumpUrl+"/question/"+id+"/comment_list";
      $.get(url,function(data){
          },'json').error(function(data) {
              var obj = $.parseJSON(data.responseText);
              layer.msg(obj.error_messages);
          }).success(function(data) {
            $(".loading").remove();
            var obj = data.data;
            var comments = obj.comments;
            console.info(obj);
            var str = '';
            // if(obj.length>9){
            //   $(".item_pager .loadMore").css("display","block");
            // }
            $(".contacts").text(lawyer_name);
            var str = '';
            if(obj.comments.length ==0 ){
              str = '<div class="noData"></div><p class="noDatafont">暂无聊天记录</p>';
              $(".ChatRecordbox").append(str);
              return false;
            }
            for (var i = 0; i < comments.length; i++) {
              if (current_id == comments[i].creator_id) {
                str+='<li class="rightChatRecord">'
                  str+='<div class="ChatRecordimg"><img src="'+comments[i].creator_avatar+'" width="54" height="54"></div>'
                  str+='<div class="contentinfo">'
                    str+='<i class="Recordicon"></i>'
                    str+='<p class="font12 colorD">'+comments[i].created_at+'</p>'
                    str+='<div class="textContent">'+comments[i].content+'</div>'
                  str+='</div>'
                str+='</li>'
              }else{
                str+='<li class="leftChatRecord">'
                  str+='<div class="ChatRecordimg fr"><img src="'+comments[i].creator_avatar+'" width="54" height="54"></div>'
                  str+='<div class="contentinfo fr">'
                    str+='<i class="Recordicon"></i>'
                    str+='<p class="font12 colorD">'+comments[i].created_at+'</p>'
                    str+='<div class="textContent">'+comments[i].content+'</div>'
                  str+='</div>'
                str+='</li>'
              }
            };
          $(".ChatRecordbox").append(str);
          $(document).on( "click", ".to_pay_ing", function(e) {
            var loading = layer.msg('加载中...', {icon: 16});
          });
              
        });
        stopDefault(e);
  });
  //弹出聊天记录
  function ChatRecord(){
    var Popup04 = $(".Popup04");
        layer.open({
          type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
          title: false, //标题
          closeBtn: true,  //关闭按钮
          shade:0.3,  //遮罩默认是0.3透明度
          area: ['570px', '610px'],
          skin: 'yourclass', //样式类名
          content: Popup04 //内容 
      });
      layerCss();
  };


 //评论
  
  comments();
  function comments() {
    //选择星星
    var index = $(".star-box .img")
      for (var i = 0; i < index.length; i++) {
        index[i].index = i;
        index[i].onclick = function() {
          imgOn(this.index);
          removeChecked(this.index);
        }
      }
     //点亮星星
    function imgOn(a) {
      index = a;
      for (var l = 0; l < index+1 ; l++) {
        $(".star-box .img").eq(l).addClass("imgSelected")
      };
      for (var n = 5; n >index; n--) {
        $(".star-box .img").eq(n).removeClass("imgSelected")
      };
    }
    // 对应选项处理
    function removeChecked(a){

      if (a == 0) {
        $(".fraction").text("1");
      }else if (a == 1) {
        $(".fraction").text("2");
      }else if (a == 2) {
        $(".fraction").text("3");
      }else if (a == 3) {
        $(".fraction").text("4");
      }else if (a == 4) {
        $(".fraction").text("5");
      }
      if (a == 0||a == 1) {
        $(".label1").css("display","block");
        $(".label2").css("display","none");
        $(".label3").css("display","none");

      }else if(a == 2){
        $(".label1").css("display","none");
        $(".label2").css("display","block");
        $(".label3").css("display","none");
      }else{
        $(".label1").css("display","none");
        $(".label2").css("display","none");
        $(".label3").css("display","block");
              }
      $(".comment-box .checked").removeClass("checked")
    }
    $(document).ready(function(){
      $(".comment-box .label1 a").toggle(
        function(){$(this).addClass("checked");},
        function(){$(this).removeClass("checked");}
      );
    });
    $(document).ready(function(){
      $(".comment-box .label2 a").toggle(
        function(){$(this).addClass("checked");},
        function(){$(this).removeClass("checked");}
      );
    });
    $(document).ready(function(){
      $(".comment-box .label3 a").toggle(
        function(){$(this).addClass("checked");},
        function(){$(this).removeClass("checked");}
      );
    });

    //服务评价
  $(document).on( "click", ".top-comment", function(e) {
    var comment_id = $(this).attr("data-href");
    var lawyer_name = $(this).attr("data-id");
    $(".subAdvisory").attr("data-id",comment_id);
    ServiceEvaluation();
    imgOn(4);
    removeChecked(4);
  });
  //弹出服务评价
  function ServiceEvaluation(){
    var Popup03 = $(".Popup03");
        layer.open({
          type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
          title: false, //标题
          closeBtn: true,  //关闭按钮
          shade:0.3,  //遮罩默认是0.3透明度
          area: ['570px', '610px'],
          skin: 'yourclass', //样式类名
          content: Popup03 //内容 
      });
      layerCss();
  };
   
      // 提交评论
      $(document).on( "click", ".subAdvisory", function(e) {
        //获取选中的标签
        var workSope0 = $(".comment-box .checked").eq(0).text();
        var workSope1 = $(".comment-box .checked").eq(1).text();
        var workSope2 = $(".comment-box .checked").eq(2).text();
        var workSope3 = $(".comment-box .checked").eq(3).text();
        var workSope4 = $(".comment-box .checked").eq(4).text();
        var evaluate_tags = {};
        if(workSope0){
          evaluate_tags['work_scope[0]'] = workSope0
        }
        if(workSope1){
          evaluate_tags['work_scope[1]'] = workSope1
        }
        if(workSope2){
          evaluate_tags['work_scope[2]'] = workSope2
        }
        if(workSope3){
          evaluate_tags['work_scope[3]'] = workSope3
        }
        if(workSope4){
          evaluate_tags['work_scope[4]'] = workSope4
        }
        var evaluate_score = $(".star-box .imgSelected").length;
        var evaluate_comment = $(".comment-box .textcon").val();
        var comment_id = $(".subAdvisory").attr("data-id");
        if (evaluate_score < 1) {
          layer.msg("您还没给评分");
          return false;
        }else if(evaluate_comment.length<1){
          layer.msg("请输入对律师的评价");
          return false;
        }else if(evaluate_comment.length>300){
          layer.msg("评价不能超过300字");
          return false;
        }else if(workSope0 == undefined||workSope0 == ""){
          layer.msg("请选择至少一个标签");
          return false;
        }
        var evaluate_url = jumpUrl+"/center/question/"+comment_id+"/evaluate";
        $.post(evaluate_url,{"evaluate_tags":evaluate_tags,"evaluate_score":evaluate_score,"evaluate_comment":evaluate_comment},function(data){
              
            },'json').error(function(data) {
                var obj = $.parseJSON(data.responseText);
                layer.msg(obj.message);
            }).success(function(data) {
                $(".comment-box .textcon").val("");
                layer.closeAll();
                layer.msg("评价成功");
                $("[data-href="+comment_id+"]").parent().parent().find(".order-status-name").text("已完成");
                $("[data-href="+comment_id+"]").parent(".butbox").html('<span class="butD top-record" data-id="'+lawyer_name+'" data-href="'+comment_id+'" style="margin-bottom: 10px;">聊天记录</span>');
            });
            stopDefault(e);
      });
  }

});
